
/*
 * Copyright (c) 2011-2023, ThinkWide (cnet2001@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thinkwide.data.ormapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页条件包装
 *
 * @author andyzhang
 * @since 2010-12-29
 */
public class MFCondition {
    private String where;
    private String orderBy;
    private Class<?> classType;
    private Integer pageSize;
    private Integer pageIndex;
    private MFDataAdapter dataAdapter;
    private List<Object> args;
    private Integer pageMax = -1;
    private Integer recordCount = -1;

    public void refreshPageMax() {
        recordCount = getCount();
    }

    public int getCount() {
        return MFActiveRecordBase.getCount(dataAdapter, classType, where, args.toArray());
    }

    public Integer getPageMax() {
        if (pageMax == -1) {
            refreshPageMax();
        }

        float f = (float) recordCount / (float) pageSize;
        pageMax = (int) Math.ceil(f);

        return pageMax;

    }

    public MFCondition(Class<?> ClassType, Integer PageSize, String OrderBy) {

        this(null, ClassType, PageSize, "", OrderBy, new Object[0]);
    }

    public MFCondition(Class<?> ClassType, Integer PageSize, String Where, String OrderBy, Object... Args) {

        this(null, ClassType, PageSize, Where, OrderBy, Args);
    }

    public MFCondition(MFDataAdapter DataAdapter, Class<?> ClassType, Integer PageSize, String Where, String OrderBy, Object... Args) {
        classType = ClassType;
        dataAdapter = DataAdapter;
        pageSize = PageSize;
        where = Where;
        orderBy = OrderBy;
        args = new ArrayList<Object>();
        setArgs(Args);

    }

    public <T> List<T> getPageData(int PageIndex) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<T> ls = (List<T>) MFActiveRecordBase.findPage(dataAdapter, this.classType, pageSize * (PageIndex - 1),
                pageSize * PageIndex, where, orderBy, args.toArray());
        return ls;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getWhere() {
        return where;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setClassType(Class<?> classType) {
        this.classType = classType;
    }

    public Class<?> getClassType() {
        return classType;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setDataAdapter(MFDataAdapter dataAdapter) {
        this.dataAdapter = dataAdapter;
    }

    public MFDataAdapter getDataAdapter() {
        return dataAdapter;
    }

    public void setArgs(Object[] args) {
        this.args.clear();
        for (Object o : args) {
            this.args.add(o);
        }
    }

    public Object[] getArgs() {
        return args.toArray();
    }
}
